package de.tu_darmstadt.gdi1.battleship.model;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;

public class Highscore {
	private BattleshipGame bsg;
	private Vector<Event> scoreList = new Vector<Event>();
	private Event lastAdded = null;

	public Highscore(BattleshipGame bsg) {
		this.bsg = bsg;
	}
/**
 * adds a score to the list
 * 
 * @return
 * true if last entry exists in the highscore
 */
	public boolean addScoreToList() {
		if (bsg.getWinner() == 1 && bsg.getEventList().size()!=0) {
			Event last = bsg.getEventList().lastElement();
			last.setName(bsg.getEventList().elementAt(0).getName());
			scoreList.addElement(last);
			sortScoreEntries();
			normList();
			if (scoreList.contains(last)) {
				setLastAdded(last);
				return true;
			}
		}
		return false;
	}

	/**
	 * @return the scoreList
	 */
	public Vector<Event> getScoreList() {
		return scoreList;
	}

	/**
	 * @param scoreList
	 *            the scoreList to set
	 */
	public void setScoreList(Vector<Event> scoreList) {
		this.scoreList = scoreList;
	}
/**
 * adds a specific event to a score list
 * @param e
 * event
 */
	public void addScoreToList(Event e){
		scoreList.add(e);
		sortScoreEntries();
		normList();
	}
	/**
	 * sorts the highscore list depending on the rules given 
	 */
	private void sortScoreEntries() {
		Collections.sort(scoreList, new Comparator<Event>() {
			@Override
			public int compare(Event o1, Event o2) {

				// Check Turns needed
				if (o1.getTurnNumber() > o2.getTurnNumber()) {
					return 1;
				} else if (o1.getTurnNumber() < o2.getTurnNumber()) {
					return -1;
				}

				// Check Time needed
				if (o1.getTurnTime() > o2.getTurnTime()) {
					return 1;
				} else if (o1.getTurnTime() < o2.getTurnTime()) {
					return -1;
				}

				// Check Play Date
				/*
				if (o1.getStartTime() > o2.getStartTime()) {
					return 1;
				} else if (o1.getStartTime() < o2.getStartTime()) {
					return -1;
				}
				*/
				if (o1.getDate().getTime() > o2.getDate().getTime()) {
					return 1;
				} else if (o1.getDate().getTime() < o2.getDate().getTime()) {
					return -1;
				}			
				

				// Check Name
				return o1.getName().compareTo(o2.getName());

			}

		});
	}

	/**
	 * norms the list to 10 entries if it has more than 10 entries
	 */
	private void normList() {
		if(scoreList.size() > 10) {
			for(int i=scoreList.size() - 1;i>=10;i--) {
				scoreList.remove(i);
			}
		}
	}
/**
 * String method especially for the output in a Dialog window(OUTDATED)
 */
	public String toString() {
		StringBuffer sb = new StringBuffer();

		sb.append("Rank /").append(" Turns needed /").append(" Time needed /")
				.append(" Game played on       /").append(" Player  \n");
		sb.append("******************************************************************\n");
		for (int i = 0; i < scoreList.size(); i++) {
			if (i == 9) {
				sb.append(i + 1)
						.append(". ")
						.append(scoreList.elementAt(i)
								.toStringForHighscoreList());

			} else {
				sb.append(" ")
						.append(i + 1)
						.append(". ")
						.append(scoreList.elementAt(i)
								.toStringForHighscoreList());
			}

		}

		return sb.toString();
	}

	/**
	 * @return the lastAdded
	 */
	public Event getLastAdded() {
		return lastAdded;
	}
	
		/**
	 * @param lastAdded the lastAdded to set
	 */
	public void setLastAdded(Event lastAdded) {
		this.lastAdded = lastAdded;
	}
	
/**
 * resets the highscore list
 */
	public void reset(){
		scoreList.clear();
	}
	public String generatesSaveString(){
		StringBuffer sb= new StringBuffer("###HSC\n");
		sb.append(scoreList.size()).append('\n');
		for (Event e : scoreList) {
			sb.append(e.generateSaveString());
		}
		return sb.toString();
		
	}
	public void loadSaveString(BufferedReader br) throws IOException {
		if (br.readLine().equals("###HSC")){
			int length = Integer.valueOf(br.readLine());
			for (int i = 0; i < length; i++) {
				Event e = null;
				e = new Event("", 0, new Coordinates(), 0, 0);
				e.loadSaveString(br);
				scoreList.add(e);}
			
			
		}else {
			System.err.println("Buffered Reader does not begin with ###HSC");
		}
	}

}
